package LeetCode;

public class Code543_diameterOfBinaryTree {

    class Info{
        int max_len;
        public Info(int m){
            max_len = m;
        }
    }

    int max = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null){
            return 0;
        }
        dfs(root);
        return max;
    }

    public Info dfs(TreeNode node){
        if (node == null){
            return null;
        }
        Info left = dfs(node.left);
        Info right = dfs(node.right);
        int left_len = 0;
        int right_len = 0;
        if (left != null){
            left_len = left.max_len;
        }
        if (right != null){
            right_len = right.max_len;
        }
        max = Math.max(max, left_len + right_len);
        return new Info(Math.max(left_len, right_len) + 1);
    }
}
